package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.NewEstVH;
import com.centaline.androidsalesblog.model.businissobject.NewEstDistrictBo;
import com.centaline.androidsalesblog.model.businissobject.NewEstGScopeBo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstDo;
import com.centaline.androidsalesblog.utils.CentaDistanceUtil;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 新盘
 * Created by guilin on 16/2/23.
 */
public class NewEstAdapter extends BaseAdapter1<NewEstVH> {

    private final DrawableRequestBuilder<String> requestBuilder;
    private final ArrayList<NewEstDo> list;
    private String newPropImg;
    private LatLng locLatLng;

    public NewEstAdapter(Context context, DrawableRequestBuilder<String> requestBuilder, ArrayList<NewEstDo> list) {
        super(context);
        this.requestBuilder = requestBuilder;
        this.list = list;
    }

    public void setNewPropImg(String newPropImg) {
        this.newPropImg = newPropImg;
    }

    public void setLocLatLng(LatLng locLatLng) {
        this.locLatLng = locLatLng;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_new_est;
    }

    @Override
    protected NewEstVH viewHolder(View view) {
        return new NewEstVH(view);
    }

    @Override
    public void onBindViewHolder(NewEstVH holder, int position) {
        final NewEstDo newEstDo = list.get(position);
        String imgAddr = newEstDo.getIconUrl();
        imgAddr = imgAddr == null || !imgAddr.contains(".") ? "" : imgAddr.replace(".", "_960x480_f.");
        GlideLoad.load(new GlideLoad.Builder(requestBuilder,
                String.format(Locale.CHINA, "%s%s", newPropImg, imgAddr))
                .placeHolder(R.drawable.ic_default_est_estate)
                .error(R.drawable.ic_default_est_estate)
                .openWifiSwitch()
                .into(holder.img_logo));
        holder.atv_price.setText(PriceUtil.newEstAvgPrice(newEstDo.getAveragePrice()));
        holder.atv_name.setText(newEstDo.getAdName());
        final NewEstDistrictBo newEstDistrictBo = newEstDo.getNewEstDistrictBo();
        final NewEstGScopeBo newEstGScopeBo = newEstDo.getNewEstGScopeBo();
        if (newEstDistrictBo != null && newEstGScopeBo != null) {
            holder.atv_desc.setText(String.format(Locale.CHINA, "%s %s",
                    newEstDistrictBo.getGScopeCnName(), newEstGScopeBo.getGScopeCnName()));
        } else {
            final StringBuilder dec = new StringBuilder();
            if (newEstDistrictBo != null) {
                dec.append(newEstDistrictBo.getGScopeCnName());
            }
            if (newEstGScopeBo != null) {
                if (dec.length() > 0) {
                    dec.append(" ");
                }
                dec.append(newEstGScopeBo.getGScopeCnName());
            }
            holder.atv_desc.setText(dec.toString());
        }

        holder.atv_est_type.setText(newEstDo.getEstType());
        CentaDistanceUtil.distanceView(holder.atv_distance, locLatLng,
                new LatLng(newEstDo.getLat(), newEstDo.getLng()));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
